package cn.fx.org.controller;

import cn.fx.basic.exception.BusinessException;
import cn.fx.basic.util.ExcelUtils;
import cn.fx.basic.util.JsonResult;
import cn.fx.basic.util.PageList;
import cn.fx.org.domain.Shop;
import cn.fx.org.domain.ShopLog;
import cn.fx.org.dto.ShopDto;
import cn.fx.org.query.ShopQuery;
import cn.fx.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
//接口文档的注解
@Api(value = "店铺的API",description="店铺相关的CRUD功能")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 接口: 查询所有
     * http://localhost:8080/shop --- url
     * 动词: GET
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping //上面的结合
    @ApiOperation(value = "查询所有店铺信息" ) //打在方法上，对方法做描述
    public List<Shop> findAll() {

        return shopService.loadAll();
    }

    /**
     * 接口: 查询单个对象
     *  http://localhost:8080/shop/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    public Shop findOne(@PathVariable("id") Long id) {

        return shopService.loadById(id);
    }

    /**
     * 接口: 分页查询
     * post - 参数通过json对象传递
     *
     * @RequestBody: 从请求体中获取数据赋值给shopQuery
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口" )
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        try {
            return shopService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 接口: 删除
     * @param id
     * @return
     *   JsonResult: 链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public JsonResult remove(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }

    }

    /**
     *  http://localhost:8080/shop
     * 接口: 添加和修改
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){
                //添加操作
                shopService.save(shop);
            } else {
                //修改操作
                shopService.update(shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            shopService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }

    }

    /**
     *
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return JsonResult.me();
        } catch(BusinessException e) {
            e.printStackTrace();
            //业务问题  e.getMessage()获取的是自定义的信息
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            //代码问题（其它不可抗拒因素）
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }
    }

    /**
     *接口：店铺审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过" )
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public JsonResult pass(@RequestBody ShopLog log) {
        try {
            shopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }

    }

    /**
     *接口：店铺审核驳回
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回" )
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public JsonResult reject(@RequestBody ShopLog log) {
        try {
            shopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public void exportExcel(HttpServletResponse response){

        try {
            //查询所有shop数据
            List<Shop> list = shopService.loadAll();
            //调用工具类
            ExcelUtils.exportExcel(list,null,"店铺数据",Shop.class,"shop.xlsx",response);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping ("/importExcel")
    @ApiOperation(value = "导入")
    public void importExcel(@RequestPart(value = "file",required = true)MultipartFile file){

        try {
            //导入 调用工具类
            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);

            //保存到数据库表中
            for (Shop shop : list) {
                shopService.save(shop);
            }

            /*修改 需要ID
            for (Shop shop : list) {
                shopService.update(shop);
            }*/

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * domain，pojo: 实体类 ----> 对应数据库中的表
     * query: 跟数据库没有直接的关系，做查询参数使用
     * dto: 跟数据库没有直接的关系，接收参数获取传参使用的
     * @return
     */
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return shopService.getEcharts();
    }
}
